import { netWork } from '@/network/request.js';

class Axios { // Axios请求构造类
  constructor () {
  }

  getRequst (url, params = null) { // GET请求函数
    return netWork({
      url,
      method: 'GET',
      params
    })
  }
  
  getPost (url, data = null) { // POST请求函数
    return netWork({
      url,
      method: 'POST',
      data
    })
  }

  getPut (url, params = null, data = null) { // POST请求函数
    // console.log(url, params);
    return netWork({
      url,
      method: 'PUT',
      params,
      data
    })
  }

  getDel (url) {
    return netWork({
      url,
      method: 'DELETE'
    })
  }

}




function getAsideDate () { // Home页面侧边栏请求
  return new Axios().getRequst('/menus');
}

function getUserListData (pagenum, pagesize, query = null) { // user组件请求user所有数据
  return new Axios().getRequst('/users', {
    pagenum,
    pagesize,
    query
  });
}

function getUserState (uId, type) { // ElTable组件修改switch组件记录状态
  return new Axios().getPut(`/users/${uId}/state/${type}`);
}

function postAddUserRole (formObj) { // ElDialog组件提交添加成员
  return new Axios().getPost('/users', formObj);
}

function getUserInfo (id) { // 获取对应id用户的信息
  return new Axios().getRequst(`users/${id}`);
}

function putUserInfo (id, infoObj) { // 根据id 修改用户的信息(email、mobile)
  return new Axios().getPut(`users/${id}`, null, infoObj);
}

function delUser(id) { // 根据用户id删除
  return new Axios().getDel(`users/${id}`);
}

function getAllRole() { //获取角色列表
  return new Axios().getRequst('roles');
}

function putUserRole(userId, roleId) { // 分配用户角色
  return new Axios().getPut(`users/${userId}/role`, null, {
    rid: roleId
  })
}

export {
  getAsideDate,
  getUserListData,
  getUserState,
  postAddUserRole,
  getUserInfo,
  putUserInfo,
  delUser,
  getAllRole,
  putUserRole
}


